package org.huangrui.juc.source.atomic;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @Author hr
 * @Create 2024-04-04 18:27
 */
public class T01_AtomicInteger {
    public static final int SIZE = 50;
    public static void main(String[] args) throws InterruptedException {
        MyNumber number = new MyNumber();
        CountDownLatch downLatch = new CountDownLatch(SIZE);
        for (int i = 1; i <= SIZE; i++) {
            new Thread(() ->{
                try {
                    for (int j = 0; j < 1000; j++) {
                         number.addPlusPlus();
                    }
                } finally {
                    downLatch.countDown();
                }
            },String.valueOf(i)).start();
        }
        //暂停1秒钟
        // try { TimeUnit.SECONDS.sleep(1);} catch (InterruptedException e) {e.printStackTrace();}
        downLatch.await();
         System.out.println(Thread.currentThread().getName()+"\t -----result "+ number.atomicInteger);
    }
}
class MyNumber
{
    AtomicInteger atomicInteger = new AtomicInteger();
    public void addPlusPlus()
    {
        atomicInteger.getAndIncrement();
    }
}